#include "Player.h"
#include "TextureManager.h"
#include "InputHangdler.h"
#include "Define.h"
#include <iostream>


Player::Player()
{
	GameObject::GameObject();

	m_radius = 10;
	right = true;
	left = false;
	up = false;
	down = false;
	m_velocity.m_x = 1;
	m_velocity.m_y = 0;

	m_idSprite = 0;
	id_magic_sprite = m_idSprite;

	timeDelay = 10;
	dt = 1;
	timeOfGame = 0;
	m_IsExpired = true;
	m_radius = 25;
}

Player::~Player() 
{
	// do nothing
}

void Player::update()
{

	timeOfGame += dt;
	if (timeOfGame > timeDelay)
	{
		m_idSprite++;
		timeOfGame = 0;
		if (m_idSprite > id_magic_sprite+1)
		{
			m_idSprite = id_magic_sprite;
		}
	}

	TheInputHangdler::Instance()->update();
	
	int ab = m_position.x;
	int ac = m_position.y;

	// direction right
	if (m_velocity.m_x == 1)
	{
		left = true;
		right = down = up = true;
		if ((TheInputHangdler::Instance()->getKeyCode() == 'a' && left == true))
		{

			TheInputHangdler::Instance()->isKeyDown();
			m_velocity.m_x = -1;
			m_velocity.m_y = 0;
			m_idSprite = id_magic_sprite = 4;
		}
		if (CheckUp(ab, ac) == WALL)
		{
			up = false;
		}
		if ((TheInputHangdler::Instance()->getKeyCode() == 'w' && up == true))
		{

			TheInputHangdler::Instance()->isKeyDown();
			m_velocity.m_x = 0;
			m_velocity.m_y = -1;
			m_idSprite = id_magic_sprite = 6;

		}
		if (CheckDown(ab, ac) == WALL && m_velocity.m_x == 1)
		{
			down = false;
		}
		if ((TheInputHangdler::Instance()->getKeyCode() == 's' && down == true))
		{
			TheInputHangdler::Instance()->isKeyDown();
			m_velocity.m_x = 0;
			m_velocity.m_y = 1;
			m_idSprite = id_magic_sprite = 2;
		}
		if (CheckRight(ab, ac) == WALL && m_velocity.m_x == 1)
		{
			right = false;
			m_velocity.m_x = -1;
			m_velocity.m_y = 0;
			m_idSprite = id_magic_sprite = 4;

		}
		
	}

	// direction left
	else if (m_velocity.m_x == -1)
	{
		right = true;
		left = down = up = true;
		if ((TheInputHangdler::Instance()->getKeyCode() == 'd'  && right == true))
		{
			TheInputHangdler::Instance()->isKeyDown();
			m_velocity.m_x = 1;
			m_velocity.m_y = 0;
			m_idSprite = id_magic_sprite = 0;
		}
		if (CheckUp(ab, ac) == WALL)
		{
			up = false;
		}
		if ((TheInputHangdler::Instance()->getKeyCode() == 'w' && up == true))
		{

			TheInputHangdler::Instance()->isKeyDown();
			m_velocity.m_x = 0;
			m_velocity.m_y = -1;
			m_idSprite = id_magic_sprite = 6;

		}
		if (CheckDown(ab, ac) == WALL && m_velocity.m_x == -1)
		{
			down = false;
		}
		if ((TheInputHangdler::Instance()->getKeyCode() == 's' && down == true))
		{
			TheInputHangdler::Instance()->isKeyDown();
			m_velocity.m_x = 0;
			m_velocity.m_y = 1;
			m_idSprite = id_magic_sprite = 2;
		}
		if (CheckLeft(ab, ac) == WALL && m_velocity.m_x == -1)
		{
			left = false;
			TheInputHangdler::Instance()->isKeyDown();
			m_velocity.m_x = 1;
			m_velocity.m_y = 0;
			m_idSprite = id_magic_sprite = 0;
		}
	}

	// direction down
	else if (m_velocity.m_y == 1)
	{
		up = true;
		right = left  = down = true;
		if ((TheInputHangdler::Instance()->getKeyCode() == 'w' && up == true))
		{

			TheInputHangdler::Instance()->isKeyDown();
			m_velocity.m_x = 0;
			m_velocity.m_y = -1;
			m_idSprite = id_magic_sprite = 6;

		}
		if (CheckLeft(ab, ac) == WALL)
		{
			left = false;
		}
		if ((TheInputHangdler::Instance()->getKeyCode() == 'a' && left == true))
		{

			TheInputHangdler::Instance()->isKeyDown();
			m_velocity.m_x = -1;
			m_velocity.m_y = 0;
			m_idSprite = id_magic_sprite = 4;
		}
		if (CheckRight(ab, ac) == WALL && m_velocity.m_y == 1)
		{
			right = false;
		}
		if ((TheInputHangdler::Instance()->getKeyCode() == 'd'  && right == true))
		{
			TheInputHangdler::Instance()->isKeyDown();
			m_velocity.m_x = 1;
			m_velocity.m_y = 0;
			m_idSprite = id_magic_sprite = 0;
		}
		if (CheckDown(ab, ac) == WALL && m_velocity.m_y == 1)
		{
			down = false;
			m_velocity.m_x = 0;
			m_velocity.m_y = -1;
			m_idSprite = id_magic_sprite = 6;
			
		}
	}
	else if (m_velocity.m_y == -1)
	{
		down = true;
		if ((TheInputHangdler::Instance()->getKeyCode() == 's' && down == true))
		{
			TheInputHangdler::Instance()->isKeyDown();
			m_velocity.m_x = 0;
			m_velocity.m_y = 1;
			m_idSprite = id_magic_sprite = 2;
		}
		right = left = up = true;
		if (CheckLeft(ab, ac) == WALL)
		{
			left = false;
		}
		if ((TheInputHangdler::Instance()->getKeyCode() == 'a' && left == true))
		{

			TheInputHangdler::Instance()->isKeyDown();
			m_velocity.m_x = -1;
			m_velocity.m_y = 0;
			m_idSprite = id_magic_sprite = 4;
		}
		if (CheckRight(ab, ac) == WALL && m_velocity.m_y == -1)
		{
			right = false;
		}
		if ((TheInputHangdler::Instance()->getKeyCode() == 'd'  && right == true))
		{
			TheInputHangdler::Instance()->isKeyDown();
			m_velocity.m_x = 1;
			m_velocity.m_y = 0;
			m_idSprite = id_magic_sprite = 0;
		}
		if (CheckUp(ab, ac) == WALL && m_velocity.m_y == -1)
		{
			up = false;
			m_velocity.m_x = 0;
			m_velocity.m_y = 1;
			m_idSprite = id_magic_sprite = 2;
		}
	}

	m_currentTime = SDL_GetTicks();
	unsigned int time;
	time = m_currentTime - m_lastTime;
	if (m_lastTime == 0)
	{
		time = 0;
	}
	
	m_speed = m_sprite.getSpriteImage()[m_idSprite].image_velocity;
	m_position.x = m_position.x + m_velocity.m_x * 2;
	m_position.y = m_position.y + m_velocity.m_y * 2;

	int x = m_position.x;
	int y = m_position.y;
	int w = 20;
	int h = 20;
	m_sprite.setDrawingCoordinates(x, y, w, h, m_idSprite);
	box.x = m_position.x;
	box.y = m_position.y;

	m_lastTime = m_currentTime;
}

void Player::clean()
{
	// do nothing
}

Point Player::getPoint() const
{
	return m_position;
}
